<?php 
class Product_model extends Model {
	
	var $tbl_name = 'product';
	var $pro_detail = 'product_detail';
	var $mem = 'member';
	var $mem_pro = 'member_pro';
	var $city = 'city';
	var $dist = 'district';
	
	function Product_model() {
		parent::Model();
	}
	
	function addProduct($ary_info=array()) {
		$this->db->insert($this->tbl_name, $ary_info);
		
		return $this->db->insert_id();
	} 
	
	function updateProduct($id=0,$ary_info) {
		//$this->db->where(array('id'=>$id));
		$this->db->where(array('id'=>$id));
		$this->db->update($this->tbl_name, $ary_info);
	} 
	
	function getProduct($ary_where=array()) {
		$this->db->select('
			p.*, pd.title,pd.condition,pd.detail_content, 
			m.email,m.member_type,m.date_created, mp.member_id,mp.company_name,mp.address mp_address,
			mp.country_id mp_country_id,mp.city_id mp_city_id,mp.district_id mp_district_id,
			mp.display_image,mp.phone mp_phone,mp.intro_content mp_intro_content,mp.website,
			c.name c_name,d.name d_name
		');
		$this->db->join($this->pro_detail.' as pd ', 'p.id=pd.product_id', 'INNER');
		$this->db->join($this->mem.' as m ', 'm.id=p.member_id', 'INNER');
		$this->db->join($this->mem_pro.' as mp ', 'm.id=mp.member_id', 'INNER');
		$this->db->join($this->city.' as c ', 'p.city_id=c.id', 'INNER');
		$this->db->join($this->dist.' as d ', 'p.district_id=d.id', 'INNER');
		$this->db->where($ary_where);
		
		$query = $this->db->get($this->tbl_name . ' as p ');
		
		if ($query->num_rows()>0) {
			return $query->row_array(); 
		}
	}
	
	function getProduct2($ary_where=array()) {
		$this->db->select('
			p.*
		');
		$this->db->where($ary_where);
		
		$query = $this->db->get($this->tbl_name . ' as p ');
		
		if ($query->num_rows()>0) {
			return $query->row_array(); 
		}
	}
	
	function getProducts($ary_where=array(),$paging=array()) {
		$this->db->select('
			p.*, pd.title,pd.condition,pd.detail_content, 
			m.email,m.member_type,m.date_created, mp.member_id,mp.company_name,mp.address mp_address,
			mp.country_id mp_country_id,mp.city_id mp_city_id,mp.district_id mp_district_id,
			mp.display_image,mp.phone mp_phone,mp.intro_content mp_intro_content,mp.website,
			c.name c_name,d.name d_name
		');
		$this->db->join($this->pro_detail.' as pd ', 'p.id=pd.product_id', 'INNER');
		$this->db->join($this->mem.' as m ', 'm.id=p.member_id', 'INNER');
		$this->db->join($this->mem_pro.' as mp ', 'm.id=mp.member_id', 'INNER');
		$this->db->join($this->city.' as c ', 'p.city_id=c.id', 'INNER');
		$this->db->join($this->dist.' as d ', 'p.district_id=d.id', 'INNER');
		$this->db->where($ary_where);
		$this->db->limit($paging['per_page'],$paging['limit']);	

		$query = $this->db->get($this->tbl_name . ' as p ');
		//echo $this->db->last_query();
		//exit;
		if ($query->num_rows()>0) {
			return $query->result_array(); 
		}
	}
	
	function getProductdetail($ary_where=array()) {
		$this->db->select('
			p.*, pd.title,pd.condition,pd.detail_content, 
			m.email,m.member_type,m.date_created, mp.member_id,mp.company_name,mp.address mp_address,
			mp.country_id mp_country_id,mp.city_id mp_city_id,mp.district_id mp_district_id,
			mp.display_image,mp.phone mp_phone,mp.intro_content mp_intro_content,mp.website,
			c.name c_name,d.name d_name
		');
		$this->db->join($this->pro_detail.' as pd ', 'p.id=pd.product_id', 'INNER');
		$this->db->join($this->mem.' as m ', 'm.id=p.member_id', 'INNER');
		$this->db->join($this->mem_pro.' as mp ', 'm.id=mp.member_id', 'INNER');
		$this->db->join($this->city.' as c ', 'p.city_id=c.id', 'INNER');
		$this->db->join($this->dist.' as d ', 'p.district_id=d.id', 'INNER');
		
		$this->db->where($ary_where);
		
		$query = $this->db->get($this->tbl_name . ' as p ');
		
		if ($query->num_rows()>0) {
			return $query->row_array();
		}
	}
	
	function countProduct($ary_where=array()) {
		$this->db->join($this->pro_detail.' as pd ', 'p.id=pd.product_id', 'INNER');
		$this->db->join($this->mem.' as m ', 'm.id=p.member_id', 'INNER');
		$this->db->join($this->mem_pro.' as mp ', 'm.id=mp.member_id', 'INNER');
		$this->db->join($this->city.' as c ', 'p.city_id=c.id', 'INNER');
		$this->db->join($this->dist.' as d ', 'p.district_id=d.id', 'INNER');
		$this->db->where($ary_where);
		return $this->db->count_all_results($this->tbl_name . ' as p ');
	}
	
	function deleteProduct($id)
	{
		$this->db->delete($this->tbl_name, array('id'=>$id));
	}
}